public class WeiboListWidget extends ElasticScrollView {

    /**
     * item鐐瑰嚮鐩戝惉鎺ュ彛
     * 
     * @author goujianzhen
     * 
     */
    public interface OnWeiboListOnClickListenner {
        public void weiboListOnClickListenner(int pocation);
    }

    public interface OnWeiboListScrollListener {
        public void scrollListener();
    }

    private static int scrolllocation = 500;
    public final static boolean DOWN_NOW = true;
    public final static boolean DOWN_END = false;
    private final String TAG = "WeiboListWidget";
    private final int DEFAULT_POSIATION = 0;
    private final int DEFAULT_HEIGHT = 0;
    private final int DEFAULT_CURRENTPAGE = 1;

    OnWeiboListScrollListener mscrollListener = null;
    LayoutInflater mInflater = null;
    WeiboListAdapter myListAdapter = null;
    Context mContext = null;
    MoreButton mroeBtn = null;
    LinearLayout totalLayout = null;
    LinearLayout overallLayout = null;
    LinearLayout mLeftLayout = null;
    LinearLayout mRightLayout = null;
    ArrayList<ImageView> mImageView1 = null;
    ArrayList<ImageView> mImageView2 = null;

    private int disPlayH = DEFAULT_HEIGHT;
    private int posiation = DEFAULT_POSIATION;
    private int leftH = DEFAULT_HEIGHT;
    private int reghtH = DEFAULT_HEIGHT;
    private int currentPage = DEFAULT_CURRENTPAGE;
    private int maxPage = 0;
    private boolean listState = false;

    public WeiboListWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setBackgroundResource(R.drawable.default_bg);// 鑳屾櫙鍥剧墖
        mContext = context;
        // init(mContext);
        createChildLayout();
        addLayout();
    }

    public WeiboListWidget(Context context) {
        super(context);
        mContext = context;
        // init(mContext);
        createChildLayout();
        addLayout();
    }

    // public WeiboListWidget(Context context) {
    // super(context);
    // mContext = context;
    // init();
    // createChildLayout();
    // addLayout();
    // }

    @Override
    protected void init(Context context) {
        super.init(context);
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // disPlayH = PreferencesUtil.getInstance().getInt(Constant.HEIGHT);
        // 鏇村叿灞忓箷楂樺害鏉ヨ绠楅珮搴�
        Log.v(TAG, "init===");
    }

    private void createChildLayout() {
        mroeBtn = new MoreButton(mContext);
        totalLayout = new LinearLayout(mContext);
        mLeftLayout = new LinearLayout(mContext);
        mRightLayout = new LinearLayout(mContext);
        overallLayout = new LinearLayout(mContext);

        setViewProperty(totalLayout, LinearLayout.VERTICAL);
        setViewProperty(mLeftLayout, LinearLayout.VERTICAL);
        setViewProperty(mRightLayout, LinearLayout.VERTICAL);

        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
        p.rightMargin = 6;
        overallLayout.setLayoutParams(p);

        // mroeBtn.setLayoutParams(new LinearLayout.LayoutParams(
        // LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
    }

    private void addLayout() {
        overallLayout.addView(mLeftLayout);
        overallLayout.addView(mRightLayout);
        totalLayout.addView(overallLayout);
        totalLayout.addView(mroeBtn);
        // this.addView(totalLayout);
        this.addChild(totalLayout, 1);
    }

    /**
     * 
     * @param layout
     * @param orientation
     */
    private void setViewProperty(LinearLayout layout, int orientation) {
        layout.setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1));
        layout.setOrientation(orientation);
    }

    public void setAdapter(WeiboListAdapter myListAdapter) {
        this.myListAdapter = myListAdapter;
        calCuurentPage();
        crateImages();
    }

    private void crateImages() {
        int count = myListAdapter.getCount();
        int i = posiation;
        for (; i < count; i++) {
            WeiboListIView weiboView = myListAdapter.getItem(i);
            int h = weiboView.height;
            if (leftH <= reghtH) {
                mLeftLayout.addView(weiboView.view);
                leftH += h;
            } else {
                mRightLayout.addView(weiboView.view);
                reghtH += h;
            }
        }
        posiation = i;
    }

    /**
     * 鑾峰緱鏄惁鏄剧ず鍒锋柊鎸夐挳鐨勭姸鎬侊紝鏀瑰彉鎸夐挳鐘舵�鐨勬搷浣滃湪Activity涓搷浣�
     * 
     * @return
     */
    public boolean getIsInTheEnd() {
        if ((currentPage) == maxPage) {
            return true;
        }
        return false;
    }

    public void setRefreshBtn() {
        mroeBtn.setMore();
    }

    public void setLoddingBtn() {
        mroeBtn.setLodding();
    }

    public void setNoMoreBtn() {
        mroeBtn.setEnd();
    }

    /**
     * 褰搒croll娌℃湁鍐呭鐨勬椂鍊欙紝浼氳Е鍙戞粴鍔ㄥ埌搴曢儴鐨勬柟娉�
     */
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        int sh = getScrollY() + getHeight();
        if (!listState && null != mscrollListener
                && sh >= computeVerticalScrollRange() && sh > disPlayH) {
            setScrollState(DOWN_NOW);
            if ((currentPage) != maxPage) {
                mscrollListener.scrollListener();
            }
        } else if (listState
                && getScrollY() + getHeight() < computeVerticalScrollRange()) {
        }
    }

    public void setOnWeiboListScrollListener(OnWeiboListScrollListener listener) {
        this.mscrollListener = listener;
    }

    public void setItemListnener(OnWeiboListOnClickListenner listenner) {
        if (null != myListAdapter) {
            myListAdapter.setItemListnener(listenner);
        }
    }

    /**
     * 璁剧疆鍋氬ぇ椤垫暟
     * 
     * @param page
     */
    public void setMaxPage(int page) {
        maxPage = page;
    }

    /**
     * 鏀瑰彉scroll鐘舵�锛屼笅杞藉畬鎴愭槸鏀规垚DOWN_END 姝ｅ湪涓嬭浇鏃舵敼鎴怐OWN_NOW DOWN_NOW
     * 
     * @param b
     */
    public void setScrollState(boolean b) {
        listState = b;
    }

    public int getCildViewCount() {
        return mLeftLayout.getChildCount() + mRightLayout.getChildCount();
    }

    /**
     * 璁剧疆More鎸夐挳鐨勭偣鍑讳簨浠�
     * 
     * @param listener
     */
    public void setMroeButtonOnClick(OnClickListener listener) {
        mroeBtn.setOnClickListener(listener);
    }

    public void setMroeOnClick(OnMBClickListener linstener) {
        mroeBtn.setMroeOnClick(linstener);
    }

    /**
     * 娓呯┖鎵�湁鍒楄〃
     */
    public void clearListView() {
        mLeftLayout.removeAllViews();
        mRightLayout.removeAllViews();
        clearViewHeight();
        this.fullScroll(ScrollView.FOCUS_UP);
    }

    public void clearViewHeight() {
        currentPage = DEFAULT_CURRENTPAGE;
        posiation = DEFAULT_POSIATION;
        leftH = DEFAULT_HEIGHT;
        reghtH = DEFAULT_HEIGHT;
        scrolllocation = 500;
    }

    /**
     * 璁＄畻褰撳墠缈婚〉椤垫暟
     */
    public void calCuurentPage() {
        int page = myListAdapter.getPage();
        int p = page % (maxPage);
        Log.v(TAG, "page===" + page);
        Log.v(TAG, "p===" + p);
        if (p == 0) {
            p = maxPage;
        }
        currentPage = p;
        Log.v(TAG, "currentPage===" + currentPage);
    }

    // public void setScrollLocation() {
    // scrolllocation = this.getScrollY();
    // }
    //
    // public void moveScrollLocation() {
    // Log.v(TAG, "scrolllocation==="+scrolllocation);
    // new Handler().post(mScrollView);
    // }
    //
    // private Runnable mScrollView = new Runnable() {
    //
    // @Override
    // public void run() {
    // WeiboListWidget.this.scrollTo(0, scrolllocation);// 鏀瑰彉婊氬姩鏉＄殑浣嶇疆
    // }
    // };
}
